from catia_tree_lib import *


def get_catia_prop(catia_path):
    """
    获取catia文件的属性信息
    :param catia_path: catia文件的本地路径
    返回示例如下：
    {
    'GL-001-B': {
        '零件编号': 'GL-001-B',
        '术语': '',
        'Object Domain ID': 'VCC',
        'Object ID': 'D15-040444219',
        'Object Title': 'Bracket'
        }
    }
    """
    catia = win32com.client.Dispatch('CATIA.Application')
    document = catia.Documents.Open(catia_path)
    # active_product = catia.ActiveDocument.Product
    result = get_tree_prop(document.Product)
    document.Close()
    return result


def get_catia_tree(catia_path):
    """
        获取catia文件的树形结构
        :param catia_path: catia文件的本地路径
        返回示例如下：
        {
            'FE060-PNL.1': {'3020re_carpart_AllCATPart.1': {}},
            'D15-040441368-00-001-Clamp-Unit': {
                'D15-040441369-00-001-Fixed-Parts': {
                    'D15-040441370-00-001-Clamp': {},
                    'D15-040441371-00-001-Clamp': {},
                    'D15-040441372-00-001-Clamp': {},
                    'D15-040441373-00-001-Clamp': {},
                    'V2_50_1_BR3_A10_T12': {},
                    'D15-040441374-00-001-Bracket': {},
                    'D15-040441375-00-001-Clamp': {},
                    '09890957 SHIM60X16X5': {},
                    '09890957 SHIM60X16X5.1': {},
                    '09890957 SHIM60X16X5.2': {},
                    '09890957 SHIM60X16X5.3': {}
                }
            }
        }
        """
    catia = win32com.client.Dispatch('CATIA.Application')
    document = catia.Documents.Open(catia_path)
    result = get_tree_relation(document.Product)
    document.Close()
    return result
    # cpprint(result)


def convert_stp(catia_path, stp_path):
    """
    将Catia产品转换成Stp文件
    :param catia_path: Catia文件路径
    :param stp_path: 转换后的Stp文件路径
    """
    catia = win32com.client.Dispatch('CATIA.Application')
    document = catia.Documents.Open(catia_path)
    document.ExportData(stp_path, "stp")
    document.Close()


if __name__ == '__main__':
    convert_stp(r"D:\project\do_design_fastapi_backend\data\DataLibrary\DataLibrary\Clamp\00cd85b11964d2ba6c2a3a8220350b77\Catia\14S.CATPart",
                r"D:\project\do_design_fastapi_backend\data\DataLibrary\DataLibrary\Clamp\00cd85b11964d2ba6c2a3a8220350b77\Catia\new_generated.stp")
